package com.aaa.zs.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @fileName:IllegalCharFilter
 * @description:
 * @author:忆尘
 * @createTime:2020/11/6 15:20
 * @version:1.0.0
 */
@Component
public class IllegalCharFilter extends ZuulFilter {

    @Value("${illegalChar}")
    private String illegalName;



    /**
     * 过滤器类型 主要是定义过滤器时机
     * @return
     */
    @Override
    public String filterType() {

        /**
         * pre 在。。。。之前  在执行业务 和我们的web filter 一致
         * route 路由 在执行业务中执行
         * post 在执行业务后，最终要调用
         * error 在执行业务发上错误是执行
         */
        return "pre";
    }

    /**
     * 当有多个过滤器时，该方法根据返回值的大小确定执行顺序，值越小，优先级越高
     * <filter>
     *
     * </filter>
     * @return
     */
    @Override
    public int filterOrder() {
        return 2;
    }


    /**
     * 确定该filter是否开启 false禁用 true开启
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤器执行业务的地方
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException{
        System.out.println(illegalName);
        //使用zuul保重提供的请求的上下文，获取上下文对象
        RequestContext currentContext = RequestContext.getCurrentContext();

        //获取request对象
        HttpServletRequest request = currentContext.getRequest();
        //获取response对象
        HttpServletResponse response = currentContext.getResponse();
        //请求http://192.168.1.6:8888/os/a/b/order/selectAll?a=1&b=2&c=3fdfd&d=4&e=fadfadsf&userName=zhangsan
        //获取所有的请求参数的name//[a,b,c,d]
        Enumeration<String> names = request.getParameterNames();

        String[] charArray = illegalName.split(",");



        //names.hasMoreElements() 判断是否有跟多的元素了
        while (names.hasMoreElements()){
            //根据名称获取参数值
            String element = names.nextElement();
            System.out.println(element+"阐述");
            String elementValue = request.getParameter(element);
            System.out.println(elementValue+"值");
            for (String illegalC : charArray){
                System.out.println(illegalC+"------十度");
                System.out.println(elementValue.equals(illegalC));
                if(elementValue.equals(illegalC)){
                    //禁止程序在线下走
                    currentContext.setSendZuulResponse(false);
                    //编码格式
                    response.setCharacterEncoding("utf-8");
                    try {
                        //返回禁止访问的信息
                        response.sendError(HttpStatus.SC_FORBIDDEN,"文明一点,我们是中国人！！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            }
        }
        return null;
    }
}
